package com.tangsm.spring.boot.event.publisher.listener.service;

import com.tangsm.spring.boot.event.publisher.listener.domain.User;
import com.tangsm.spring.boot.event.publisher.listener.event.UserRegisterEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/**
 * 事件发布者
 * @author tangsm
 */
@Service
public class UserRegisterService implements ApplicationEventPublisherAware {

	private ApplicationEventPublisher applicationEventPublisher;

	/**
	 * 用户注册
	 * @param user 用户信息
	 * @return 成功失败
	 */
	public boolean register(User user) {
		//用户注册
		System.out.println("[service]用户["  + user + "]注册成功！");

		//事件发布者，发布消息
		applicationEventPublisher.publishEvent(new UserRegisterEvent(this, user));
		return true;
	}


	/**
	 * Set the ApplicationEventPublisher that this object runs in.
	 * <p>Invoked after population of normal bean properties but before an init
	 * callback like InitializingBean's afterPropertiesSet or a custom init-method.
	 * Invoked before ApplicationContextAware's setApplicationContext.
	 *
	 * @param applicationEventPublisher event publisher to be used by this object
	 */
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}
}
